package exam.hadoop;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
/*文件偏移量   hang  date    temp*/


public class mapper extends Mapper<LongWritable, Text, Text, FloatWritable> {
    private Text dateKey = new Text();
    private FloatWritable temperature = new FloatWritable();

    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        if (line.startsWith("STN---")) {
            return;
        }

        String[] fields = line.split("\\s+");
        if (fields.length > 3) {
            String tempStr = fields[3];
            try {
                double tempFahrenheit = Double.parseDouble(tempStr);
                double tempCelsius = (tempFahrenheit - 32) * 5 / 9;
                if (tempCelsius >= 10 && tempCelsius <= 35) {
                    dateKey.set(fields[2]);
                    temperature.set((float) tempCelsius);
                    context.write(dateKey, temperature);
                }
            } catch (NumberFormatException e) {
                // 记录日志或忽略异常
            }
        }
    }
}